package bavaria.hightech.timer;

import java.util.Calendar;
import java.util.Timer;
import java.util.TimerTask;

public class InternalClock {
	private static InternalClock timerInstance;
	private static Calendar time;
	private static final int TIMEFACTOR = 86400; //2592000 entspricht 1 Monat pro Sekunde
	private static Timer timer;
	
	/*
	 * Implements the Singleton. Method is private in order to have only one instance
	 */
	private InternalClock(){
		time = Calendar.getInstance();
		startTiming();
	}
	
	public static InternalClock getInstance(){
		if (timerInstance == null)
			timerInstance = new InternalClock();
		return timerInstance;
		
	}
	
	public static Calendar getCalendar(){
		return (Calendar) time.clone();
	}
		
	private void modifyTimeObject() {
		time.add(Calendar.SECOND, TIMEFACTOR);
//		System.out.println(zeit.getTime());
	}
	private void startTiming() {
		timer = new Timer();
		timer.scheduleAtFixedRate(
				new TimerTask()  {
					public void run()  {
						InternalClock.this.modifyTimeObject();
					}
				},
				0, 1000);
	}
	
	public static void startClock() {
		new Clock(time, timer);
	}
	
	public static void startCheckPayInterestTimerTask(int periodInDays, TimerTask timerTask) {
		timer.scheduleAtFixedRate(timerTask,0 , periodInDays*86400/TIMEFACTOR);
	}
}